<template>
  <div class="container">
    <!-- 头部菜单 -->
    <div class="order-tit">
      <span @click="onStsTap" data-sts="0" :class="sts == 0 ? 'on' : ''"
        >全部</span
      >
      <span @click="onStsTap" data-sts="1" :class="sts == 1 ? 'on' : ''"
        >待支付</span
      >
      <span @click="onStsTap" data-sts="2" :class="sts == 2 ? 'on' : ''"
        >待发货</span
      >
      <span @click="onStsTap" data-sts="3" :class="sts == 3 ? 'on' : ''"
        >待收货</span
      >
      <span @click="onStsTap" data-sts="4" :class="sts == 4 ? 'on' : ''"
        >已完成</span
      >
    </div>
    <!-- 订单列表 -->
    <div class="main">
      <div class="empty" v-if="orderList.length == 0">还没有任何相关订单</div>
      <div v-if="orderList.length > 0">
        <div v-for="(item, i) in orderList" :key="i">
          <div class="prod-item">
            <div class="order-num">
              <span :data-ordernum="item.orderNumber"
                >订单编号：{{ item.orderNumber }}</span
              >
              <div class="order-state">
                <!-- :class="[item.status==4 ? order-sts.red : order-sts.gray]" -->
                <span
                  :class="item.status == 6 ? 'order-sts.gray' : 'order-sts.red'"
                  >{{
                    item.status == 1
                      ? "待支付"
                      : item.status == 2
                      ? "待发货"
                      : item.status == 3
                      ? "待收货"
                      : item.status == 4
                      ? "已完成"
                      : "已取消"
                  }}</span
                >

                <div
                  class="clear-btn"
                  v-if="item.status == 5 || item.status == 6"
                >
                  <img
                    @click="delOrderList(item)"
                    src="/static/images/icon/clear-his.png"
                    class="clear-list-btn"
                  />
                </div>
              </div>
            </div>

            <div v-for="(items, index) in item.orderItemDtos" :key="index">
              <div class="item-cont" @click="toOrderDetail(item)">
                <div class="prod-pic">
                  <img :src="items.pic" />
                </div>
                <div class="prod-info">
                  <div class="prodname">
                    {{ items.prodName }}
                  </div>
                  <div class="prod-info-cont">{{ items.skuName }}</div>
                  <div class="price-nums">
                    <sapn class="prodprice">
                      <text class="symbol">￥</text>
                      <sapn class="big-num">{{ items.price }}</sapn>
                      <sapn class="small-num">.0</sapn>
                    </sapn>
                    <sapn class="prodcount">x{{ items.prodCount }}</sapn>
                  </div>
                </div>
              </div>
              <div class="total-num">
                <span class="prodcount">共1件商品</span>
                <div class="prodprice">
                  合计<span class="symbol">￥</span>
                  <span class="big-num">1</span>
                  <span class="small-num">.01</span>
                </div>
              </div>
            </div>
            <div class="prod-foot">
              <div class="btn">
                <span
                  @click="onCancelOrder(item)"
                  :class="item.status == 6 ? '' : 'button'"
                  >{{ item.status == 6 ? "" : "取消订单" }}</span
                >
                <span :class="item.status == 6 ? '' : 'button warn'">{{
                  item.status == 6 ? "" : "再次购买"
                }}</span>
                <span
                  v-if="item.status == 1"
                  @click="onPayAgain(item)"
                  class="button warn"
                  :data-ordernum="item.orderNumber"
                  >付款</span
                >
                <span v-if="item.status == 6" class="button warn"
                  >在此购买</span
                >
                <span
                  v-if="item.status == 3 || item.status == 5"
                  class="button"
                  @click="toDeliveryPage"
                  :data-ordernum="item.orderNumber"
                  hover-class="none"
                  >查看物流</span
                >
                <span
                  v-if="item.status == 3"
                  class="button warn"
                  @click="onConfirmReceive"
                  :data-ordernum="item.orderNumber"
                  hover-class="none"
                  >确认收货</span
                >
              </div>
            </div>
          </div>
        </div>
      </div>
      <div v-else>
        <div class="item-cont">
          <scroll-view
            :scroll-x="true"
            :scroll-left="0"
            :scroll-with-animation="false"
            class="categories"
          >
            <div v-for="(ite, index) in item.orderItemDtos" :key="index">
              <view class="prod-pic">
                <image :src="ite.pic"></image>
              </view>
            </div>
          </scroll-view>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      sts: 0,
      list: [],
      payamentid: "",
    };
  },
  onLoad(e) {
    if (e.sts) {
      this.sts = e.sts;
    }
    this.getActionsOrder(this.sts);
  },
  computed: {
    ...mapState({
      orderList: (state) => state.user.orderList,
      payamentList: (state) => state.user.payamentList,
    }),
  },
  methods: {
    ...mapActions({
      getActionsOrder: "user/getActionsOrder",
      getActionsDelete: "user/getActionsDelete",
      getActionsCancel: "user/getActionsCancel",
      getActionsPayament: "user/getActionsPayament",
    }),
    // 付款
    async onPayAgain(item) {
      this.payamentid = item.orderNumber;
      await this.getActionsPayament({
        orderNumbers: this.payamentid,
        payType: 1,
      });
      wx.requestPayment({
        timeStamp: this.payamentList.timeStamp,
        nonceStr: this.payamentList.nonceStr,
        package: this.payamentList.packageValue,
        signType: "MD5",
        paySign: this.payamentList.paySign,
        success(res) {
          console.log("");
          wx.navigateTo({
            url: "/pages/pay-result/index?sts=1",
          });
        },
        fail(res) {
          console.log("支付失败");
           wx.navigateTo({
            url: "/pages/pay-result/index?sts=0",
          });
        },
      });
    },
    //点击是否取消订单
    onCancelOrder(item) {
      wx.showModal({
        title: "",
        content: "要取消此订单？",
        confirmColor: "#3e62ad",
        cancelColor: "#3e62ad",
        cancelText: "否",
        confirmText: "是",
        success: async (res) => {
          if (res.confirm) {
            await this.getActionsCancel(item.orderNumber);
            await this.getActionsOrder(this.sts);
          } else if (res.cancel) {
            //console.log('用户点击取消')
          }
        },
      });
    },
    // 点击tab切换
    onStsTap(e) {
      var sts = e.currentTarget.dataset.sts;
      this.sts = sts;
      this.getActionsOrder(this.sts);
    },
    //点击删除订单
    delOrderList(item) {
      wx.showModal({
        title: "",
        content: "确定要删除此订单吗？",
        confirmColor: "#eb2444",
        success: async (res) => {
          if (res.confirm) {
            await this.getActionsDelete(item.orderNumber);
            await this.getActionsOrder(this.sts);
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    },
    toOrderDetail(item) {
      wx.navigateTo({
        url: "/pages/orderDetail/index?orderNum=" + item.orderNumber,
      });
    },
  },
  mounted() {
    this.getActionsOrder(this.sts);
    this.getActionsPayament({
      orderNumbers: this.payamentid,
      payType: 1,
    });
  },
};
</script>

<style scoped>
.container {
  height: 100%;
  box-sizing: border-box;
  color: #333;
  font-family: helvetica, "Heiti SC", PingFangSC-Light;
}
.order-tit {
  position: fixed;
  top: 0;
  display: flex;
  justify-content: space-around;
  z-index: 999;
  width: 100%;
  height: 100rpx;
  line-height: 100rpx;
  background-color: #fff;
  border-bottom: 2rpx solid #f4f4f4;
  font-size: 28rpx;
}
.on {
  color: #eb2444;
  border-bottom: 4rpx solid #eb2444;
}
.main {
  margin-top: 100rpx;
}
.empty {
  font-size: 24rpx;
  margin-top: 100rpx;
  text-align: center;
  color: #999;
  height: 300rpx;
  line-height: 300rpx;
}
.prod-item {
  background-color: #fff;
  margin-top: 15rpx;
  font-size: 28rpx;
}
.prod-item .order-num .clear-btn .clear-list-btn {
  width: 100%;
  height: 100%;
  vertical-align: middle;
}
.order-num {
  padding: 20rpx 30rpx;
  display: flex;
  justify-content: space-between;
  font-size: 28rpx;
}
.order-state {
  display: flex;
  align-items: center;
  font-size: 24rpx;
}
.order-state .order-sts {
  margin-left: 100rpx;
}
.prod-item .order-num .clear-btn {
  width: 32rpx;
  height: 32rpx;
  font-size: 0;
  vertical-align: top;
  margin-left: 60rpx;
  position: relative;
}
.prod-item .item-cont {
  display: flex;
  align-items: center;
  padding: 20rpx 30rpx;
  border-radius: 10rpx;
  display: -webkit-flex;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  background: #fafafa;
}
.prod-item .item-cont .prod-pic {
  font-size: 0;
  display: inline-block;
  width: 160rpx;
  height: 160rpx;
  overflow: hidden;
  background: #fff;
  margin-right: 16rpx;
}
.prod-item .item-cont .prod-info {
  margin-left: 10rpx;
  font-size: 28rpx;
  width: 100%;
  position: relative;
  height: 160rpx;
  -webkit-flex: 1;
  -ms-flex: 1;
  -webkit-box-flex: 1;
  -moz-box-flex: 1;
  flex: 1;
}
.prod-item .item-cont .prod-info .prodname {
  font-size: 28rpx;
  line-height: 36rpx;
  max-height: 86rpx;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  text-overflow: ellipsis;
  word-break: break-all;
}
.prod-item .item-cont .prod-info .prod-info-cont {
  color: #999;
  line-height: 40rpx;
  margin-top: 10rpx;
  font-size: 22rpx;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  text-overflow: ellipsis;
  word-break: break-all;
}
.prod-item .price-nums .prodprice {
  color: #333;
  position: absolute;
  bottom: 0;
}
.symbol {
  font-size: 24rpx;
}
.big-num {
  font-size: 32rpx;
}
.small-num {
  font-size: 24rpx;
}
.prod-item .price-nums .prodcount {
  position: absolute;
  bottom: 5rpx;
  right: 0;
  color: #999;
  font-family: verdana;
}
.prod-item .item-cont .prod-pic image {
  width: 100%;
  height: 100%;
}
.order-state .order-sts.red {
  color: #eb2444;
}

.order-state .order-sts.gray {
  color: #999;
}
.prod-item .prod-foot {
  padding: 20rpx 30rpx;
  border-top: 2rpx solid #e6e6e6;
}
.prod-item .prod-foot .btn {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
.button {
  margin-left: 10px;
  font-size: 26rpx;
  background: #fff;
  padding: 10rpx 30rpx;
  border-radius: 80rpx;
  border: 2rpx solid #e1e1e1;
}
.button.warn {
  color: #eb2444;
  border-color: #eb2444;
}
.prod-item .total-num {
  text-align: right;
  padding: 20rpx 30rpx;
  font-size: 28rpx;
}
.prod-item .total-num .prodcount {
  margin-right: 20rpx;
}
.prod-item .total-num .prodprice {
  display: inline-block;
  color: #333;
}
.symbol {
  font-size: 24rpx;
}
.big-num {
  font-size: 32rpx;
}
.small-num {
  font-size: 24rpx;
}
</style>
